package mc2;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@SuppressWarnings("serial")
public class User implements java.io.Serializable,Comparable<User>{
    public static final User Anonymous=new User();
    public static final char STATUS_NOTAUTH='0';
    public static final char STATUS_ASKINGNICK='1';
    public static final char STATUS_NICKFAILED='2';
    public static final char STATUS_AUTH='3';
        
    private final String _name;
    private boolean _anonymous=false;
    private char _status=STATUS_NOTAUTH;
    private List<String> msgHistory = new CopyOnWriteArrayList<String>();
    private static final int HISTORY_SIZE = 100;
    private long lastActivity = System.currentTimeMillis();
   
    
    public boolean IsAnonymous(){
        return _anonymous;
    }
    
    public char GetStatus(){
        return _status;
    }
    
    public void SetStatus(char iStatus){
        _status=iStatus;
    }
    
    public String GetName(){
        return _name;
    }
    /** Creates a new instance of User */
    public User(String iName) {
        _name=iName;
    }
    
    //Creates an anonymous user
    private User(){
        _anonymous=true;
        _name="???";
    }
    
    public boolean equals(Object obj){
        if(!(obj instanceof User))
            return false;
        if(IsAnonymous() || ((User)obj).IsAnonymous()) return false;
        
        return _name.equals(((User)obj)._name);
    }
    
    
    
    public String toString(){
        return _name;
    }
    
    public long getLastActivity() {
      return lastActivity;
    }
    
    public void setLastActivity(long lastActivity) {
      this.lastActivity = lastActivity;
    }
    
    @Override
    public int compareTo(User iTo){
        if(! (iTo instanceof User))
            return 0;
        return _name.compareTo(((User)iTo)._name);
    }
    
    public List<String> getMsgHistory() {
      return Collections.unmodifiableList(msgHistory);
    }
    
    public void addMsgHistory(String... msgs) {
      
      while(msgHistory.size() > HISTORY_SIZE) {
        msgHistory.remove(0);
      }
      
      if(msgs != null) {
        for(String msg : msgs) {
          if(msg != null) {
            msgHistory.add(msg);
          }
        }
      }
    }
}
